package com.gui.windows.components;

import com.gui.beans.*;
import com.gui.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

/**
 * Singleton Class that manages the tools panel.
 * */
public class ToolPanel extends JToolBar implements GuiComponents{

	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************		
	
	private static ToolPanel toolPanel = null;

	private ToolPanel(String name) {
		super();
		createPanel(name);	
	}
	
	public static ToolPanel getToolPanel (String name) {
		if (toolPanel == null) {
			toolPanel = new ToolPanel(name);
		}
		return toolPanel;
	}
	
	//**************************************************************************************************
	//	Atributes.
	//**************************************************************************************************
	
	// all available button commands on toolPanel.
	private static final String ADD = "add";
	private static final String EDIT = "edit";
	private static final String DEL = "delete";
	
	// variable to set the tool panel enablement.
	private static boolean enabled = true;

	//**************************************************************************************************
	//	Methods that exports this Class (Public Methods).
	//**************************************************************************************************
    
    /**
     * Changes the tool panel enablement.
     * @param
     * */
    public void setEnabled (boolean enabled) {
    	this.enabled = enabled;
    	updateComponentsEnablement();
    }
    
    public void update() {}

    //**************************************************************************************************
	//	Inner Methods. (Private Methods).
	//**************************************************************************************************
    
    private void createPanel (String name) {
		this.setName(name);
        JButton button = null;
        button = createToolButton("add", ADD, "Adds the components selected in the seleccion panel.", "Add component");
        this.add(button);
        button = createToolButton("edit", EDIT, "Edits the components selected in the seleccion panel.", "Edit component");
        this.add(button);
        button = createToolButton("delete", DEL, "Deletes the components selected in the seleccion panel.", "Delete component");
        this.add(button);
        this.setBorder(BorderFactory.createEtchedBorder());
    }
	
	/**
	 * Updates the tool panel enablement according to the static variable {@link enabled}.
	 * */
	private void updateComponentsEnablement () {
		for (int i = 0 ; i < this.getComponentCount() ; ++i ) {
			this.getComponent(i).setEnabled(enabled);
		}
	}
	
	/**
	 * Creates and configures a new button.
	 * @param
	 * <p>
	 * @return JButton
	 * */
    private JButton createToolButton (String imageName, String actionCommand, String toolTipText, String altText) {
    	String imgLocation = Resources.ICON_RESOURCES + imageName + Resources.ICON_SIZE + Resources.ICON_EXTENSION;
        System.out.println(imgLocation);
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.setIcon(new ImageIcon(imgLocation, altText));
        if (actionCommand.equals(ADD)) {
        	button.addActionListener(new AddComponentHandler());
        } else if (actionCommand.equals(EDIT)) {
        	button.addActionListener(new EditComponentHandler());
        } else if (actionCommand.equals(DEL)) {
			button.addActionListener(new DeleteComponentHandler());
        } 
        button.setEnabled(false);
		return button;
    }
    
    //**************************************************************************************************
	//*  Buttons Handlers Classes
	//**************************************************************************************************
    
    private class AddComponentHandler implements ActionListener {
    	public void actionPerformed (ActionEvent e) {
    		System.out.println("Adding a component");
    		//TODO: Implementar este handler para agregar componentes al template.
    		//Se comunicara a traves de una interfaz con la clase Template 
    	}
	}
	
	private class EditComponentHandler implements ActionListener {
    	public void actionPerformed (ActionEvent e) {
    		System.out.println("Editing a component");
    		//TODO: Implementar este handler para editar componentes del template.
    		//Se comunicara a traves de una interfaz con la clase Template
		}
	}
	
	private class DeleteComponentHandler implements ActionListener {
    	public void actionPerformed (ActionEvent e) {
    		System.out.println("Deleting a component");
    		//TODO: Implementar este handler para borrar componentes del template.
    		//Se comunicara a traves de una interfaz con la clase Template
    	}
	}
}

